package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.R;
import com.ruoyi.common.core.domain.model.Broker;
import com.ruoyi.common.core.domain.model.Image;
import com.ruoyi.common.core.domain.model.Store;
import com.ruoyi.mapper.BrokerMapper;
import com.ruoyi.mapper.ImageMapper;
import com.ruoyi.service.IBrokerService;
import com.ruoyi.service.IStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * 经纪人Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-13
 */
@Service
public class BrokerServiceImpl extends ServiceImpl<BrokerMapper,Broker>implements IBrokerService
{
    @Autowired
    private BrokerMapper brokerMapper;
    @Autowired
    IStoreService iStoreService;
    @Autowired
    ImageMapper imageMapper;
    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     * 查询经纪人
     * 
     * @param id 经纪人主键
     * @return 经纪人
     */
    @Override
    public Broker selectBrokerById(String id)
    {
        return brokerMapper.selectBrokerById(id);
    }

    /**
     * 查询经纪人列表
     * 
     * @param broker 经纪人
     * @return 经纪人
     */
    @Override
    public List<Broker> selectBrokerList(Broker broker)
    {
        return brokerMapper.selectBrokerList(broker);
    }

    public String Brokeriid(){
        //使用redis的主键自增策略
        //现在的时间
        String s = UUID.randomUUID().toString();
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        Long key = redisTemplate.opsForValue().increment("key");
        return "zj"+format+s+key;
    }

    /**
     * 新增经纪人
     * 
     * @param broker 经纪人
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBroker(Broker broker)
    {
        //通过门店的id查询 门店的名称
        Store byId = iStoreService.getById(broker.getStoreId());
        if(Objects.isNull(byId)){
            throw new RuntimeException("门店不存在");
        }
        broker.setStoreName(byId.getName());
        broker.setId(Brokeriid());
        int i = brokerMapper.insert(broker);
        //把经纪人资格证书 存入到 数据库中
        for (Image image : broker.getImageList()) {

            image.setId(broker.getId());

            imageMapper.insert(image);
        }
        return i;
    }

    /**
     * 修改经纪人
     * 
     * @param broker 经纪人
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBroker(Broker broker)
    {
        Store byId = iStoreService.getById(broker.getStoreId());
        if(Objects.isNull(byId)){
            throw new RuntimeException("门店不存在");
        }
        broker.setStoreName(byId.getName());
        broker.setStoreName(byId.getName());
        broker.setId(broker.getId());
        int i = brokerMapper.updateBroker(broker);
        //把经纪人资格证书 存入到 数据库中
        //首先 根据id 去删除
        List<Image> imageList = broker.getImageList();
        for (Image image : imageList) {
            image.setId(broker.getId());
            imageMapper.deleteById(image.getId());
        }
        for (Image image : broker.getImageList()) {

            image.setId(broker.getId());

            imageMapper.insert(image);
        }

        return i;
    }

    /**
     * 批量删除经纪人
     * 
     * @param ids 需要删除的经纪人主键
     * @return 结果
     */
    @Override
    public int deleteBrokerByIds(String[] ids)
    {
        return brokerMapper.deleteBrokerByIds(ids);
    }

    /**
     * 删除经纪人信息
     * 
     * @param id 经纪人主键
     * @return 结果
     */
    @Override
    public int deleteBrokerById(String id)
    {
        return brokerMapper.deleteBrokerById(id);
    }

    @Override
    public R InquireBroker(Broker broker) {

     List<Broker>list=brokerMapper.InquireBroker(broker);

     return R.OK(list);
    }
}
